Skip to content

feat: add Object::GetPrototype and Object::SetPrototype#1715

Open
KevinEady wants to merge 1 commit intonodejs:mainfrom
KevinEady:add-get-set-prototype
Open

feat: add Object::GetPrototype and Object::SetPrototype#1715
KevinEady wants to merge 1 commit intonodejs:mainfrom
KevinEady:add-get-set-prototype

Conversation

@KevinEady
Copy link
Contributor

  • Add Object::GetPrototype and Object::SetPrototype
  • Update docs and tests

Fixes: #1691

@codecov-commenter
Copy link

codecov-commenter commented Mar 4, 2026

Codecov Report

❌ Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 63.51%. Comparing base (fe0c48e) to head (db1379a).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
napi-inl.h 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1715   +/-   ##
=======================================
  Coverage   63.50%   63.51%           
=======================================
  Files           3        3           
  Lines        2047     2050    +3     
  Branches      728      729    +1     
=======================================
+ Hits         1300     1302    +2     
  Misses        162      162           
- Partials      585      586    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

### GetPrototype()

```cpp
Napi::Object Napi::Object::GetPrototype() const;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The prototype of the method is MaybeOrValue<Object> Object::GetPrototype() const.

Maybe it could be good to explain if the object has no prototype.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @NickNaso ! 😄

If the object has no prototype (i.e. a null prototype), the Maybe would have a value of null, which is a valid Value-having Maybe.

If using NODE_ADDON_API_ENABLE_MAYBE:

  • Passing null or undefined to Object::GetPrototype(), then the Maybe's value is Nothing.

If not using NODE_ADDON_API_ENABLE_MAYBE:

  • Passing null or undefined to GetPrototype will throw a TypeError: Cannot convert undefined or null to object.

I'm not sure if this needs to be specifically documented, as this is just how Maybes work?

const prototype = {};
const obj = Object.create(prototype);
assert.strictEqual(binding.object.getPrototype(obj), prototype);
}
Copy link
Member

@NickNaso NickNaso Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be useful to test when the oblect has no prototype const obj = Object.create(null); ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@legendecas legendecas moved this from Need Triage to In Progress in Node-API Team Project Mar 13, 2026
@KevinEady KevinEady force-pushed the add-get-set-prototype branch from aa0f8c0 to db1379a Compare March 13, 2026 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

Safer way to set object prototype

4 participants